package model.semantics;

import model.State;
import model.parser.ParserException;

/**
 * This node, given a variable and an expression, evaluates the expression and
 * sets the variable to the constant expression value.
 * 
 * @author Sterling Dorminey
 * 
 * @param <T>
 */
public class MakeNode<T> extends Node<T> {
    private VariableNode<T> myVariable;
    private Node<T> myValue;

    public MakeNode(Node<T> variable, Node<T> value) {
        if (!(variable instanceof VariableNode)) {
            throw ParserException.BAD_ARGUMENTS;
        }
        myVariable = (VariableNode<T>) variable;
        myValue = value;
    }

    @Override
    public T evaluate(State<T> state) {
        // Evaluate the value in the context of the current state.
        T out = myValue.evaluate(state);

        state.setAssignment(myVariable.getLabel(), new ConstNode<T>(out));
        return out;
    }

}
